package com.qbw.app.module.user.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.http.okhttp.HttpTask;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.UserFragmentRegistAfterVerifycodeBinding;
import com.qbw.app.module.common.activity.WebViewActivity;
import com.qbw.app.module.common.fragment.CommonFragment;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.app.module.user.activity.UserRegistActivity;
import com.qbw.app.module.vehicle.activity.VehicleBindByScanActivity;
import com.qbw.app.request.impl.UserRequest;
import com.qbw.core.observable.ObservableBoolean;
import com.qbw.core.observable.ObservableString;

/**
 * @author QBW
 * @date 2019/9/23
 */
public class UserRegistAfterVerifyCodeFragment extends CommonFragment<UserFragmentRegistAfterVerifycodeBinding> {

    private ObservableString mPasswordFirst = new ObservableString();
    private ObservableString mPassword = new ObservableString();
    private ObservableString mError = new ObservableString();
    private ObservableBoolean mReadProtocol = new ObservableBoolean();

    public static UserRegistAfterVerifyCodeFragment createInstance() {
        UserRegistAfterVerifyCodeFragment ins = new UserRegistAfterVerifyCodeFragment();
        return ins;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.user_fragment_regist_after_verifycode;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserRegistActivity activity = (UserRegistActivity) getActivity();
        mBinding.setPasswordFirst(mPasswordFirst);
        mBinding.setPassword(mPassword);
        mBinding.setError(mError);
        mBinding.setReadProtocol(mReadProtocol);
        mBinding.executePendingBindings();
        mBinding.tvNext.setOnClickListener(this);
        mBinding.tvProtocol.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_next) {
            if (!mReadProtocol.isValue()) {
                UniversalToastUtil.showToast(R.string.please_read_protocol);
                return;
            } else if (mPasswordFirst.getLength() < 6 || mPassword.getLength() < 6) {
                UniversalToastUtil.showToast(R.string.input_password);
                return;
            } else if (!mPasswordFirst.getValue().equals(mPassword.getValue())) {
                UniversalToastUtil.showToast(R.string.password_not_equal);
                return;
            }
            UserRegistActivity activity = (UserRegistActivity) getActivity();
            mGeneralManager.getRequestManager()
                           .getUserRequest()
                           .regist(activity.getCountryArea().getValue(),
                                   activity.getPhone().getValue(),
                                   mPassword.getValue(),
                                   activity.getVerifyCode().getData(),
                                   this)
                           .execute();
        } else if (vid == R.id.tv_protocol) {
            WebViewActivity.start(getActivity(), "", Constant.URL_USER_PROTOCOL, 0, "");
        }
    }

    @Override
    public void onHttpStart(HttpTask httpTask) {
        super.onHttpStart(httpTask);
        String method = httpTask.getMethod();
        if (method.equals(UserRequest.METHOD_REGIST)) {
            showLoadingDialog("");
        }
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        super.onHttpSuccess(httpTask, entity);
        String method = httpTask.getMethod();
        if (method.equals(UserRequest.METHOD_REGIST)) {
            closeLoadingDialog();
            VehicleBindByScanActivity.startActivity(getActivity(), VehicleBindByScanActivity.TYPE_FROM_NO_VEHICLE);
            getActivity().finishAffinity();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (method.equals(UserRequest.METHOD_REGIST)) {
            closeLoadingDialog();
            mError.setValue(message);
        }
    }
}
